function NetWorkUtils() {

  //构造函数
  function NetWorkUtils() {
    return this
  }

  /**
   * url 请求地址
   * success 成功的回调
   * fail 失败的回调
   */
  NetWorkUtils.prototype._get = function (url, success, fail) {
    console.log("------start---_get----");
    wx.request({
      url: url,
      header: {
        // 'Content-Type': 'application/json'
      },
      success: function (res) {
        success(res);
      },
      fail: function (res) {
        fail(res);
      }
    });

    console.log("----end-----_get----");
  }

  /**
   * url 请求地址
   * success 成功的回调
   * fail 失败的回调
   */
  NetWorkUtils.prototype._post = function (url, data, success, fail) {
    console.log("----_post--start-------");
    wx.request({
      url: url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: { data: data },
      success: function (res) {
        success(res);
      },
      fail: function (res) {
        fail(res);
      }
    });
    console.log("----end-----_get----");
  }


  /**
   * url 请求地址
   * success 成功的回调
   * fail 失败的回调
   */
  NetWorkUtils.prototype._post1 = function (that, url, data, success, fail) {
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: data,
      success: function (res) {
        success(that, res);
      },
      fail: function (res) {
        fail(that, res);
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  }

  /**
   * 错误日志上传
   */
  NetWorkUtils.prototype._error = function (url, data) {
    wx.showLoading({
      title: '',
    })
    console.log(data)
    wx.request({
      url: url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: data,
      success: function (res) {
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  }

  /**
  * url 请求地址
  * success 成功的回调
  * fail 失败的回调
  */
  NetWorkUtils.prototype._post_json = function (that, url, data, success) {
    this._post_page_json(that, url, 0, data, success)
  }


  /**
  * 可分页
  * url 请求地址
  * success 成功的回调
  * fail 失败的回调
  */
  NetWorkUtils.prototype._post_page_json = function (that, url, start, data, success) {

    // data.userId = "cYkC00eE4pA9GdDQamOwY6";
    // data.tenantId = "86vBwgVIAu88rW4Kt+-vFx";
    // data.openId = "ofpoh0c0qGik0dHB7gVJHSniQFbg";
    // data.appId = "wx9e6171322e500e67";
    // this._post_request(that, url, start, data, success);

    let app = getApp();
    if (app.globalData.openId) {
      data.openId = app.globalData.openId;
      data.userId = app.globalData.userId;
      data.tenantId = app.globalData.tenantId;
      this._post_request(that, url, start, data, success);
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userAndTenantInfoCallback = res => {
        data.openId = app.globalData.openId;
        data.userId = res.id;
        data.tenantId = res.tenant.id;
        this._post_request(that, url, start, data, success);
      }
    }
  }

  /**
  * 可分页
  * that 页面指针
  * url 请求地址
  * start 从多少条数据开始求请
  * data 求请参数
  * success 成功的回调
  */
  NetWorkUtils.prototype._post_request = function (that, url, start, data, success) {
    console.log(url);
    console.log(JSON.stringify(data));
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: url + "?start=" + start + "&length=20",
      // header: {
      //     'content-type': 'application/json',
      // },
      method: 'POST',
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.status == 1) {
          success(that, res);
        } else if (res.data.status == 2) {
          if (res.data.data) {
            that.showZanTopTips(that.error, res.data.data);
            //that.showZanToast(res.data.data);
          } else {
            //that.showZanToast(res.data.message);
            that.showZanTopTips(that.error, res.data.message);
          }
        } else {
          that.showZanTopTips(that.error, '未找到服务');
          //that.showZanToast('未找到服务');
        }
      },
      fail: function (res) {
        that.showZanTopTips(that.error, '网络连接出错');
        //that.showZanToast('网络连接出错');
      },
      complete: function () {
        wx.hideLoading();
      },
    });
  }

  NetWorkUtils.prototype._post_product_request = function (that, url, start, data, success) {

    // data.userId = "cYkC00eE4pA9GdDQamOwY6";
    // data.tenantId = "eccnJuWxk-H8bpbS+FcBwH";
    // data.openId = "ofpoh0c0qGik0dHB7gVJHSniQFbg";
    // data.appId = "wx9e6171322e500e67";
    // // //wxeaf7672d43934e24
    // this.post_product_request(that, url, start, data, success);
    var app = getApp();
    if (app.globalData.openId) {
      data.openId = app.globalData.openId;
      data.userId = app.globalData.userId;
      data.tenantId = app.globalData.tenantId;
      this.post_product_request(that, url, start, data, success);
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userAndTenantInfoCallback = res => {
        data.openId = app.globalData.openId;
        data.userId = res.id;
        data.tenantId = res.tenant.id;
        this.post_product_request(that, url, start, data, success);
      }
    }
  }

  /**
  * 商品
  * that 页面指针
  * url 请求地址
  * start 从多少条数据开始求请
  * data 求请参数
  * success 成功的回调
  */
  NetWorkUtils.prototype.post_product_request = function (that, url, start, data, success) {
    console.log(url);
    console.log(JSON.stringify(data));

    wx.showLoading({
      title: '',
    })
    wx.request({
     // url: url + "?start=" + start + "&length=20" + '&order[0][column]=1&order[0][dir]=' + orderType + '&columns[1][data]=' + order,
      url: url + '?start=' + start + '&length=20' + '&order[0][column]=1&order[0][dir]=ASC&columns[1][data]=orderNo',
      method: 'POST',
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.status == 1) {
          success(that, res);
        } else {
          that.showZanTopTips(that.error, res.data);
        }
      },
      fail: function (res) {
        that.showZanTopTips(that.error, res.data);
      },
      complete: function () {
        wx.hideLoading();
      },
    });
  }

  return new NetWorkUtils();
}

module.exports = {
  NetWorkUtils: NetWorkUtils
}
